package com.example.takeoff.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.log.Log;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.takeoff.common.Result;
import com.example.takeoff.common.constants;
import com.example.takeoff.controller.dto.userDto;
import com.example.takeoff.entity.DDL;
import com.example.takeoff.exception.ProjectExceptionAdvice;
import com.example.takeoff.exception.ServiceException;
import com.example.takeoff.mapper.DDLMapper;
import com.example.takeoff.service.userService;
import com.example.takeoff.utils.*;
import com.example.takeoff.entity.user;
import com.example.takeoff.mapper.userMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class userServiceImpl extends ServiceImpl<userMapper, user> implements userService
{
    @Autowired
    private userMapper userMapper;


    @Override
    public user Register(userDto userDto)
    {
        LambdaQueryWrapper<user> qw = new LambdaQueryWrapper<>();
        //检查是否已经注册
        qw.eq(user::getUsername,userDto.getUsername());
        user one;
        try{
            one=userMapper.selectOne(qw);
        }catch (Exception e){
            Log.get().error(e);
            throw new ServiceException(constants.code_500,"系统错误");
        }
        if(one!=null)
            throw new ServiceException(constants.code_700,"用户名重复");
        //校验密码是否合法
        boolean check = checkPassword.check(userDto.getPassword());
        if(!check)
            throw new ServiceException(constants.code_800,"密码不合规");

        //检验重复密码
        if(!userDto.getPassword2().equals(userDto.getPassword()))
            throw new ServiceException(constants.code_900,"密码和重复密码不同");
        //手机号长度为11
        if(userDto.getPhoneNum().length()!=11)
            throw new ServiceException(constants.code_1000,"手机号长度不为11");
        user one2=new user();
        BeanUtil.copyProperties(userDto,one2);

        IdGenerator idGenerator = new IdGenerator(0,0);
        long l = idGenerator.nextId();
        one2.setUserid(String.valueOf(l));
        one2.setPassword(md5.md5(userDto.getPassword()));
        userMapper.insert(one2);
        return one2;
    }

    @Override
    public userDto Login(userDto userDto)
    {
        LambdaQueryWrapper<user> qw = new LambdaQueryWrapper<>();
        qw.eq(user::getUsername, userDto.getUsername());
        qw.eq(user::getPassword, md5.md5(userDto.getPassword()));
        user one;
        try
        {
            one = userMapper.selectOne(qw);
        }
        catch (Exception e)
        {
            Log.get().error(e);
            throw new ServiceException(constants.code_500, "系统错误");
        }
        if (one != null)
        {
            BeanUtil.copyProperties(one, userDto, true);
            String token = tokenUtil.genToken(one.getUserid(), one.getPassword());
            userDto.setToken(token);
            return userDto;
        }
        else
        {
            throw new ServiceException(constants.code_600, "用户名或密码错误");
        }
    }


}
